#!/bin/bash
echo "------- This script should be placed in the parent directory of 'IEdgeinsights' -------"
echo "Changing the source file..."
env_file="IEdgeInsights/build/.env"
sed -i 's/DEV_MODE=.*/DEV_MODE=true/g' $env_file
eii_version=$(cat $env_file | grep -i EII_VERSION | awk -F '=' '{print $2}')
# sed -i 's!OPENVINO_IMAGE=.*!OPENVINO_IMAGE=openvino/ubuntu20_data_runtime:2021.4!g' $env_file
pre_requisitesfile="IEdgeInsights/build/pre_requisites.sh"
sed -i 's!google.com!baidu.com!g' $pre_requisitesfile
sed -i 's!https://github.com/docker/compose/releases/download/${REQ_DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)!https://gitee.com/xiaowei315/eii_3rd_pkg/attach_files/853031/download/docker-compose-$(uname -s)-$(uname -m)-${REQ_DOCKER_COMPOSE_VERSION}!g' $pre_requisitesfile

common_dockerfile="IEdgeInsights/common/dockerfiles/Dockerfile.common"
sed -i 's!git clone https://github.com/xeipuuv!git clone https://gitee.com/mirrors_xeipuuv!g' $common_dockerfile
sed -i 's!git clone https://github.com/golang/glog!git clone https://gitee.com/mirrors_openshift/golang-glog!g' $common_dockerfile

common_source_dockerfile="IEdgeInsights/common/dockerfiles/Dockerfile.sources"
sed -i 's!https://github.com/minio/minio!https://gitee.com/mirrors/minio!g' $common_source_dockerfile
sed -i 's!https://github.com/OpenPeeDeeP/depguard!https://gitee.com/mirrors_openpeedeep/depguard!g' $common_source_dockerfile
sed -i 's!https://github.com/golangci/check!https://gitee.com/mirrors_golangci/check!g' $common_source_dockerfile
sed -i 's!https://github.com/golangci/golangci-lint!https://gitee.com/mirrors/GolangCI-Lint!g' $common_source_dockerfile
sed -i 's!https://github.com/minio/console!https://gitee.com/mirrors_minio/console!g' $common_source_dockerfile
sed -i 's!https://github.com/minio/direct-csi!https://gitee.com/mirrors_minio/direct-csi!g' $common_source_dockerfile
sed -i 's!https://github.com/minio/kes!https://gitee.com/mirrors_minio/kes!g' $common_source_dockerfile
sed -i 's!https://github.com/minio/mc!https://gitee.com/mirrors_minio/mc!g' $common_source_dockerfile
sed -i 's!https://github.com/minio/operator!https://gitee.com/mirrors_minio/operator!g' $common_source_dockerfile
sed -i 's!https://github.com/minio/pkg!https://gitee.com/mirrors_minio/pkg!g' $common_source_dockerfile
sed -i 's!https://github.com/golang/freetype!https://gitee.com/mirrors_golang/freetype!g' $common_source_dockerfile
sed -i 's!https://github.com/zeromq/libzmq!https://gitee.com/mirrors_back/libzmq!g' $common_source_dockerfile

common_util_istlfile="IEdgeInsights/common/util/c/install.sh"
sed -i 's!https://github.com/netmail-open/wjelement!https://gitee.com/xiaowei315/wjelement!g' $common_util_istlfile

provision_dockerfile="IEdgeInsights/build/provision/dep/Dockerfile"
lib_installfile="IEdgeInsights/common/eii_libs_installer.sh"
sed -i 's!git clone https://github.com/etcd-io!git clone https://gitee.com/mirrors!g' $lib_installfile
sed -i 's!git clone https://github.com/golang/glog!git clone https://gitee.com/mirrors_openshift/golang-glog!g' $lib_installfile
webvis_dockerfile="IEdgeInsights/WebVisualizer/Dockerfile"
sed -i 's!code.jquery.com!s3.pstatp.com/cdn/expire-1-M/jquery/3.4.1!g' $webvis_dockerfile
sed -i 's/jquery-3.4.1.min.js/jquery.min.js/g' $webvis_dockerfile

# add go proxy
EtcdUI_dockerfile="IEdgeInsights/EtcdUI/Dockerfile"
spec_line=`cat $EtcdUI_dockerfile | grep -n 'go build' | awk -F ":" '{print $1}'`
add_line=$(( $spec_line-1 ))
sed -i "$add_line i ENV GOPROXY https://goproxy.io" $EtcdUI_dockerfile

# add pip source
grep -nr "install.* -r .*requirements.txt" | grep -i IEdgeInsights | grep -v README | awk -F ":" '{print$1}' | xargs sed -i 's!install.* -r .*requirements.txt!& -i https://mirrors.aliyun.com/pypi/simple!g'

grep -nr "pip install -U pip" | grep -i IEdgeInsights | grep -v README | awk -F ":" '{print$1}' | xargs sed -i 's!pip install -U pip!& -i https://mirrors.aliyun.com/pypi/simple!g'

influx_dockerfile="IEdgeInsights/InfluxDBConnector/Dockerfile"
sed -i 's!https://github.com/influxdata/influxdb!https://gitee.com/mirrors_back/influxdb!g' $influx_dockerfile
kapaci_dockerfile="IEdgeInsights/Kapacitor/Dockerfile"
sed -i 's!https://github.com/influxdata!https://gitee.com/mirrors!g' $kapaci_dockerfile
sed -i 's!-c intel!-c https://mirrors.aliyun.com/anaconda/cloud/intel!g' $kapaci_dockerfile
sed -i 's!https://repo.anaconda.com!https://mirrors.tuna.tsinghua.edu.cn/anaconda!g' $kapaci_dockerfile
telegraf_dockerfile="IEdgeInsights/Telegraf/Dockerfile"
sed -i 's!golang.org/dl!golang.google.cn/dl!g' $telegraf_dockerfile
sed -i 's!https://github.com/influxdata/telegraf!https://gitee.com/mirrors_influxdata/telegraf!g' $telegraf_dockerfile
grpc_line=`cat $telegraf_dockerfile | grep -n 'google.golang.org/grpc' | awk -F ":" '{print $1}'`
goproxyadd_line=$(( $grpc_line-1 ))
sed -i "$goproxyadd_line i ENV GOPROXY https://goproxy.cn" $telegraf_dockerfile
imagestore_dockerfile="IEdgeInsights/ImageStore/Dockerfile"
sed -i 's!https://github.com/mitchellh/go-homedir!https://gitee.com/mirrors_addons/go-homedir!g' $imagestore_dockerfile
sed -i 's!https://github.com/golang/crypto!https://gitee.com/mirrors/crypto!g' $imagestore_dockerfile
sed -i 's!https://github.com/minio/minio-go!https://gitee.com/mirrors_minio/minio-go!g' $imagestore_dockerfile
sed -i 's!https://github.com/golang/net!https://gitee.com/virnop/golang-net!g' $imagestore_dockerfile
sed -i 's!https://github.com/golang/text!https://gitee.com/virnop/golang-text!g' $imagestore_dockerfile
sed -i 's!https://github.com/golang/sys!https://gitee.com/virnop/golang-sys!g' $imagestore_dockerfile
sed -i 's!https://github.com/go-ini/ini!https://gitee.com/mirrors/ini.git!g' $imagestore_dockerfile

multus_file="IEdgeInsights/build/helm-eii/gige_setup/multus_setup.sh"
sed -i 's!https://github.com/intel/multus-cni!https://gitee.com/mirrors_intel/multus-cni!g' $multus_file
safestring_file="IEdgeInsights/common/util/c/IntelSafeString/cmake/IntelSafeString.txt.in"
sed -i 's!https://github.com/intel/safestringlib!https://gitee.com/mirrors_back/safestringlib!g' $safestring_file
find IEdgeInsights/OpcuaExport/OpcuaBusAbstraction -type f -name "Makefile" | xargs sed -i 's!https://github.com/intel/safestringlib!https://gitee.com/mirrors_intel/safestringlib!g'

grep -nr "curl -L --retry 5 https://github.com/.*.gz" | awk -F ":" '{print$1}' | xargs sed -i 's!curl -L https://github.com!curl -L --retry 5 https://github.com!'

find IEdgeInsights/common -type f -name "install.sh" | xargs sed -i 's!wget!wget --tries 5!g'

grep -nr "https://github.com/coreos.*.gz" | awk -F ":" '{print$1}' | xargs sed -i 's!https://github.com/coreos/etcd/releases/download/${ETCD_VERSION}/etcd-${ETCD_VERSION}-linux-amd64.tar.gz!https://gitee.com/xiaowei315/eii_3rd_pkg/attach_files/786635/download/etcd-v${ETCD_VERSION}-linux-amd64.tar.gz!g'

grep -nr "https://github.com/DaveGamble.*.gz" | awk -F ":" '{print$1}' | xargs sed -i 's!https://github.com/DaveGamble/cJSON/archive/v${cjson_version}.tar.gz!https://gitee.com/xiaowei315/eii_3rd_pkg/attach_files/786633/download/cJSON-${cjson_version}.tar.gz!g'

if [ "$eii_version" == "2.6" ];then
    grep -nr "https://github.com/zeromq.*.gz" | awk -F ":" '{print$1}' | xargs sed -i 's!https://github.com/zeromq/libzmq/releases/download/v4.3.1/zeromq-${zeromq_version}.tar.gz!https://gitee.com/xiaowei315/eii_3rd_pkg/attach_files/786634/download/zeromq-${zeromq_version}.tar.gz!g'
else
    grep -nr "https://github.com/zeromq.*.gz" | awk -F ":" '{print$1}' | xargs sed -i 's!https://github.com/zeromq/libzmq/releases/download/v4.3.3/zeromq-${zeromq_version}.tar.gz!https://gitee.com/xiaowei315/eii_3rd_pkg/attach_files/892628/download/zeromq-${zeromq_version}.tar.gz!g'
fi
grep -nr "https://github.com/IntelRealSense.*.gz" | awk -F ":" '{print$1}' | xargs sed -i 's!https://github.com/IntelRealSense/librealsense/archive/v${LIBREALSENSE_VERSION}.tar.gz!https://gitee.com/xiaowei315/eii_3rd_pkg/attach_files/791838/download/librealsense-${LIBREALSENSE_VERSION}.tar.gz!g'

grep -nr "https://github.com/twbs/.*.zip" | awk -F ":" '{print$1}' | xargs sed -i 's!https://github.com/twbs/bootstrap/releases/download/v4.0.0/bootstrap-4.0.0-dist.zip!https://gitee.com/xiaowei315/eii_3rd_pkg/attach_files/786714/download/bootstrap-4.0.0-dist.zip!g'

echo "Change Done."
